#include "phylip_fitch/fitch.cpp"
#include "phylip_fitch/dist.cpp"
#include "phylip_fitch/phylip.cpp"
#include "phylip_fitch/read_phylip_tree.cpp"


// standard exceptions
#include <stdexcept>

// boost headers
#include <boost/tuple/tuple.hpp>

// component headers
#include <tree.h>

using std::logic_error;


namespace fitch_tree
{

//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

//	Helper functions without progress dots

//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
void fitch_make_nhstr(  const bcpp_matrix::t_matrix_f&		io,
						t_fitch_params&						params,
						t_fitch_results&					results)
{
	ostringstream ignore_strm;	t_progress_indicator dots(ignore_strm, 10000);
	fitch_make_nhstr(  io, params, results, dots);
}

void optimize_nhstr(	const std::string&					tree_in_nh,
						const bcpp_matrix::t_matrix_f&		io,
						t_fitch_params_optimise&			params,
						t_fitch_results&					results)
{
	ostringstream ignore_strm;	t_progress_indicator dots(ignore_strm, 10000);
	optimize_nhstr(	   tree_in_nh, io, params, results, dots);
}



void fitch_make_tree(	bcpp_tree::t_tree& 				resulting_tree,
						const bcpp_matrix::t_matrix_f&		io,
						t_fitch_params&						params,
						t_fitch_results&					results)
{
	ostringstream ignore_strm;	t_progress_indicator dots(ignore_strm, 10000);
	fitch_make_tree( resulting_tree, io, params, results, dots);
}



//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

//	Helper functions to make actual tree

//8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
void fitch_make_tree(	bcpp_tree::t_tree& 				resulting_tree,
						const bcpp_matrix::t_matrix_f&		io,
						t_fitch_params&						params,
						t_fitch_results&					results,
						t_progress_indicator&				dots)
{
	fitch_make_nhstr(io, params, results, dots );

	string error;
	bool success;
	boost::tie(success, error) = load_tree_from_newhampshire_format_str(results.nh_tree, resulting_tree);
	if (!success)
		throw std::logic_error("Fitch tree failed to parse [" + error + "]\n" +
								results.nh_tree + "\n");
}

}
